//
//  FileUtil.m
//
//  Created by 宋鹏程 on 14-4-8.
//  Copyright (c) 2014年 Master. All rights reserved.
//

#import "FileUtil.h"

@implementation FileUtil

+ (BOOL)existFile:(NSString *)v_path
{
    NSFileManager *fm = [NSFileManager defaultManager];
    return [fm fileExistsAtPath:v_path];
}

+ (NSData *)readFile:(NSString *)v_path
{
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:v_path]) {
        return nil;
    } else {
		return [fm contentsAtPath:v_path];
	}
}

+ (BOOL)writeFile:(NSData * )v_fileData andPath:(NSString *)v_path
{
    if (nil == v_fileData) {
        return NO;
    }
	return [v_fileData writeToFile:v_path atomically:YES];
}

+ (BOOL)deleteFile:(NSString *)v_path
{
    NSFileManager *fm = [NSFileManager defaultManager];
	return [fm removeItemAtPath:v_path error:nil];
}

+ (NSString *)getDocumentsFilePath:(NSString *)v_path andName:(NSString *)v_name
{
    NSArray *directoryArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = directoryArray[0];
    NSString *fileFolder = [documentDir stringByAppendingPathComponent:v_path];
    NSString *filePath = fileFolder;
    if (nil != v_name) {
        filePath = [filePath  stringByAppendingPathComponent:v_name];
    }
    
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:fileFolder]) {
        NSError *error;
        [fm createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:&error];
    }
    return filePath;
}

+ (NSString *)getCachesFilePath:(NSString *)v_path andName:(NSString *)v_name
{
    NSArray *directoryArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = directoryArray[0];
    NSString *fileFolder = [cachesDir stringByAppendingPathComponent:v_path];
    NSString *filePath = fileFolder;
    if (nil != v_name) {
        filePath = [filePath stringByAppendingPathComponent:v_name];
    }
    
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:fileFolder]) {
        NSError *error;
        [fm createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:&error];
    }
    return filePath;
}



+ (NSString *)getTmpFilePath:(NSString *)v_path andName:(NSString *)v_name
{
    NSString *temporaryDir = NSTemporaryDirectory();
    NSString *fileFolder = [temporaryDir stringByAppendingPathComponent:v_path];
    NSString *filePath = fileFolder;
    if (nil != v_name) {
        filePath = [filePath  stringByAppendingPathComponent:v_name];
    }
    
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:fileFolder]) {
        NSError *error;
        [fm createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:&error];
    }
    return filePath;
}

+ (NSArray *)getDirectoryContentsAtPath:(NSString *)FolderPath
{
    NSFileManager * fm = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *fileContent = [fm contentsOfDirectoryAtPath:FolderPath error:&error];
    return fileContent;
}

+ (NSArray *)getSomeTypeFileAtPath:(NSString *)FolderPath type:(NSString *)type
{
    NSFileManager * fm = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *fileContent = [fm contentsOfDirectoryAtPath:FolderPath error:&error];
    NSMutableArray *typefileArray = [NSMutableArray arrayWithCapacity:1];
    for (int i = 0; i < fileContent.count; i++) {
        NSString *fileName = fileContent[i];
//        NSString *fileNamePath = [FolderPath stringByAppendingPathComponent:fileName];
//        NSDictionary *atribute = [fm attributesOfItemAtPath:fileNamePath error:nil];
//        NSString *extension = [atribute fileType];
        NSString *extension = [fileName pathExtension];
        if ([extension isEqualToString:type]) {
            [typefileArray addObject:fileContent];
        }
    }
    return typefileArray;
}

+ (BOOL)moveFile:(NSString *)fromPath toPath:(NSString *)toPath
{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error;
    if ([fm fileExistsAtPath:toPath]) {
        [fm removeItemAtPath:toPath error:&error];
    }
    return [fm moveItemAtPath:fromPath toPath:toPath error:&error];
}
@end
